#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
const int N = 1010;
typedef pair<int,int> PII;
char s[N][N];
int a[N][N];
int n,m;
int dx[] = {0,0,-1,1};
int dy[] = {-1,1,0,0};
void bfs()
{
    
    queue<PII> q;
    memset(a,-1,sizeof a);
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= m; j++)
        {
            if(s[i][j] == '1')
            {
                q.push({i,j});
                a[i][j] = 0;
            }
        }
    }
    
    while(q.size())
    {
        auto t = q.front();q.pop();
        int i = t.first , j = t.second;
        for(int k = 0; k < 4; k++)
        {
            int x = i + dx[k] , y = j + dy[k];
            if(x <= n && x >= 1 && y <= m && y >= 1 && a[x][y] == -1)
            {
                
                a[x][y] = a[i][j] + 1;
                q.push({x,y});
                
            }
        }
    }
}
int main()
{
    cin >> n >> m;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            cin >> s[i][j];

    bfs();
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= m; j++)
        {
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}
